package com.nsgf.library.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
import android.widget.Scroller;
import androidx.appcompat.widget.AppCompatTextView;

import com.zhht.htzx.R;

public class MarqueeTextView extends AppCompatTextView{
private Scroller mScroller;
private int mScrollSpeed = 10; // 默认滚动速度
private boolean mIsScrolling = false;
private boolean mAutoStart = true; // 是否自动开始滚动
private int mTextWidth = 0;
private int mViewWidth = 0;
private boolean mIsFirstScroll = true;
private String mOriginalText = ""; // 保存原始文本
private int mScrollGap = 50; // 文字之间的间隔

public MarqueeTextView(Context context) {
    super(context);
    init(context, null);
}

public MarqueeTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context, attrs);
}

public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context, attrs);
}

private void init(Context context, AttributeSet attrs) {
    if (attrs != null) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MarqueeTextView);
        mScrollSpeed = typedArray.getInteger(R.styleable.MarqueeTextView_scrollSpeed, 50);
        mAutoStart = typedArray.getBoolean(R.styleable.MarqueeTextView_autoStart, true);
        mScrollGap = typedArray.getInteger(R.styleable.MarqueeTextView_scrollGap, 50);
        typedArray.recycle();
    }

    mScroller = new Scroller(context, new LinearInterpolator());
    setSingleLine();
    setHorizontallyScrolling(true);
}

@Override
public void setText(CharSequence text, BufferType type) {
    mOriginalText = text.toString();
    // 只显示一次文本，添加一个间隔
    String displayText = mOriginalText + " ".repeat(mScrollGap / 2);
    super.setText(displayText, type);
    requestLayout();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    mViewWidth = getMeasuredWidth();
    mTextWidth = (int) getPaint().measureText(mOriginalText);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    if (mAutoStart && !mIsScrolling) {
        startScroll();
    }
}

@Override
public void computeScroll() {
    super.computeScroll();
    if (mScroller.computeScrollOffset()) {
        scrollTo(mScroller.getCurrX(), 0);
        postInvalidate();
    } else if (mIsScrolling) {
        // 当滚动到一定位置时，重置到开始位置并继续滚动
        if (getScrollX() >= mTextWidth + mScrollGap) {
            scrollTo(0, 0);
        }
        startScroll();
    }
}

public void startScroll() {
    if (!mIsScrolling) {
        mIsScrolling = true;
        int startX = mIsFirstScroll ? 0 : getScrollX();
        // 计算一个完整的滚动周期（一个文本宽度加上间隔）
        int scrollDistance = mTextWidth + mScrollGap;

        mScroller.startScroll(startX, 0, scrollDistance, 0, mScrollSpeed * 1000);
        invalidate();

        if (mIsFirstScroll) {
            mIsFirstScroll = false;
        }
    }
}

public void stopScroll() {
    if (mIsScrolling) {
        mIsScrolling = false;
        mScroller.abortAnimation();
        scrollTo(0, 0);
        mIsFirstScroll = true;
    }
}

public void pauseScroll() {
    if (mIsScrolling) {
        mIsScrolling = false;
        mScroller.abortAnimation();
    }
}

public void resumeScroll() {
    if (!mIsScrolling) {
        mIsScrolling = true;
        int currentX = getScrollX();
        int totalWidth = mTextWidth + mScrollGap;
        int remainingDistance = totalWidth - currentX;
        mScroller.startScroll(currentX, 0, remainingDistance, 0, mScrollSpeed * 1000);
        invalidate();
    }
}

public void setScrollSpeed(int speed) {
    if (speed > 0) {
        this.mScrollSpeed = speed;
        if (mIsScrolling) {
            pauseScroll();
            resumeScroll();
        }
    }
}

public void setScrollGap(int gap) {
    if (gap >= 0) {
        this.mScrollGap = gap;
        setText(mOriginalText); // 重新设置文本以应用新的间隔
    }
}

@Override
protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    stopScroll();
}
}